package com.zib.vread.sinaweibo.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zib.vread.sinaweibo.R;
import com.zib.vread.sinaweibo.api.Constants;
import com.zib.vread.sinaweibo.base.BaseActivity;
import com.zib.vread.sinaweibo.mvp.view.activity.login.LoginActivity;
import com.zib.vread.sinaweibo.mvp.view.fragment.unlogin.UnLoginFindFragment;
import com.zib.vread.sinaweibo.mvp.view.fragment.unlogin.UnLoginHomeFragment;
import com.zib.vread.sinaweibo.mvp.view.fragment.unlogin.UnLoginMessageFragment;
import com.zib.vread.sinaweibo.mvp.view.fragment.unlogin.UnLoginProfileFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UnLoginActivity extends BaseActivity {

    @Bind(R.id.contentLayout)
    FrameLayout contentLayout;
    @Bind(R.id.tv_home)
    RelativeLayout tvHome;
    @Bind(R.id.tv_message)
    RelativeLayout tvMessage;
    @Bind(R.id.fl_post)
    ImageView flPost;
    @Bind(R.id.tv_find)
    RelativeLayout tvFind;
    @Bind(R.id.tv_profile)
    RelativeLayout tvProfile;
    private Context mContext = this;
    private UnLoginHomeFragment mHomeFragment;
    private UnLoginMessageFragment mMessageFragment;
    private UnLoginFindFragment mFindFragment;
    private UnLoginProfileFragment mProfileFragment;
    private FragmentManager mFragmentManager;
    private int mCurrentIndex = -1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_un_login;
    }

    @Override
    public void initView() {
        mFragmentManager = getSupportFragmentManager();
        setTabFragment(0);
        setUpListener();
    }

    private void setUpListener() {
        tvHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabFragment(0);

            }
        });
        tvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabFragment(1);
            }
        });
        flPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ToastUtil.showShort(mContext, "请先登录");
            }
        });
        tvFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabFragment(3);

            }
        });
        tvProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabFragment(4);
            }
        });
    }


    private void setTabFragment(int index) {
        if (mCurrentIndex != index) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            hideAllFragments(transaction);
            switch (index) {
                case 0:
                    tvHome.setSelected(true);
                    if (mHomeFragment == null) {
                        mHomeFragment = new UnLoginHomeFragment();
                        transaction.add(R.id.contentLayout, mHomeFragment);
                    } else {
                        transaction.show(mHomeFragment);
                    }
                    mCurrentIndex = 0;
                    break;
                case 1:
                    tvMessage.setSelected(true);
                    if (mMessageFragment == null) {
                        mMessageFragment = new UnLoginMessageFragment();
                        transaction.add(R.id.contentLayout, mMessageFragment);
                    } else {
                        transaction.show(mMessageFragment);
                    }
                    mCurrentIndex = 1;
                    break;

                case 3:
                    tvFind.setSelected(true);
                    if (mFindFragment == null) {
                        mFindFragment = new UnLoginFindFragment();
                        transaction.add(R.id.contentLayout, mFindFragment);
                    } else {
                        transaction.show(mFindFragment);
                    }
                    mCurrentIndex = 3;
                    break;
                case 4:
                    tvProfile.setSelected(true);
                    if (mProfileFragment == null) {
                        mProfileFragment = new UnLoginProfileFragment();
                        transaction.add(R.id.contentLayout, mProfileFragment);
                    } else {
                        transaction.show(mProfileFragment);
                    }
                    mCurrentIndex = 4;
                    break;
            }
            transaction.commit();
        } else if (mCurrentIndex == 0 && mHomeFragment != null) {

        }
    }

    private void hideAllFragments(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            transaction.hide(mHomeFragment);
        }
        if (mMessageFragment != null) {
            transaction.hide(mMessageFragment);
        }

        if (mFindFragment != null) {
            transaction.hide(mFindFragment);
        }
        if (mProfileFragment != null) {
            transaction.hide(mProfileFragment);
        }
        tvHome.setSelected(false);
        tvMessage.setSelected(false);
        tvFind.setSelected(false);
        tvProfile.setSelected(false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    public void openLoginWebView(View view) {
        String authurl = "https://open.weibo.cn/oauth2/authorize" + "?" + "client_id=" + Constants.APP_KEY
                + "&response_type=token&redirect_uri=" + Constants.REDIRECT_URL
                + "&key_hash=" + Constants.AppSecret + (TextUtils.isEmpty(Constants.PackageName) ? "" : "&packagename=" + Constants.PackageName)
                + "&display=mobile" + "&scope=" + Constants.SCOPE;

        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.putExtra("url", authurl);
        startActivity(intent);
        finish();
    }

}
